import { request } from '@/api/service'

export const crudOptions = vm => {
  return {
    pageOptions: {
      compact: true
    },
    options: {
      tableType: 'vxe-table',
      rowKey: true, // 必须设置，true or false
      rowId: 'id',
      height: '100%', // 表格高度100%, 使用toolbar必须设置
      highlightCurrentRow: false
    },
    rowHandle: {
      width: 140,
      view: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Retrieve')
        }
      },
      edit: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Update')
        }
      },
      remove: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Delete')
        }
      }
    },
    indexRow: {
      // 或者直接传true,不显示title，不居中
      title: '序号',
      align: 'center',
      width: 100
    },
    viewOptions: {
      componentType: 'form'
    },
    formOptions: {
      defaultSpan: 24, // 默认的表单 span
      width: '35%'
    },
    columns: [{
      title: 'ID',
      key: 'id',
      show: false,
      disabled: true,
      width: 90,
      form: {
        disabled: true
      }
    },
    {
      title: '关键字',
      key: 'search',
      show: false,
      disabled: true,
      search: {
        disabled: false
      },
      form: {
        disabled: true,
        component: {
          placeholder: '请输入关键词'
        }
      },
      view: {
        disabled: true
      }
    },
    {
      title: '故障类型',
      key: 'fault_type',
      treeNode: true,
      type: 'select',
      search: {
        disabled: false
      },
      dict: {
        url: '/api/system/dictionary/' + '?limit=999&status=1',
        cache: true,
        value: 'value', // 数据字典中value字段的属性名
        label: 'label', // 数据字典中label字段的属性名
        getData: (url, dict, { form, component }) => { // 配置此参数会覆盖全局的getRemoteDictFunc
          return request({ url: url }).then(ret => {
            const responseData = ret.data.data
            return responseData.filter(data => data.code === 'fault_type')
          })
        }
      },
      form: {
        editDisabled: true,
        rules: [
          // 表单校验规则
          { required: true, message: '故障类型必填' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入故障类型'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '故障组',
      key: 'fault_group',
      type: 'select',
      search: {
        disabled: false
      },
      dict: {
        url: '/api/system/dictionary/' + '?limit=998&status=1',
        cache: true,
        value: 'value', // 数据字典中value字段的属性名
        label: 'label', // 数据字典中label字段的属性名
        getData: (url, dict, { form, component }) => { // 配置此参数会覆盖全局的getRemoteDictFunc
          return request({ url: url }).then(ret => {
            const responseData = ret.data.data
            return responseData.filter(data => data.code === 'fault_group')
          })
        }
      },
      form: {
        editDisabled: true,
        rules: [
          // 表单校验规则
          { required: true, message: '故障组必填' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入故障组'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '故障位',
      key: 'fault_site',
      type: 'input',
      form: {
        editDisabled: true,
        rules: [
          // 表单校验规则
          { required: true, message: '故障位必填' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入故障位'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '故障标签',
      key: 'fault_label',
      type: 'select',
      form: {
        editDisabled: true,
        rules: [
          // 表单校验规则
          { required: true, message: '故障标签必填' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请选择故障标签'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '故障码',
      key: 'fault_code',
      type: 'input',
      form: {
        editDisabled: true,
        rules: [
          // 表单校验规则
          { required: true, message: '故障码必填' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请选择故障码'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '是否警告',
      key: 'is_warn',
      type: 'select',
      dict: {
        data: [{ value: 0, label: '正常' }, { value: 1, label: '警告' }]
      },
      form: {
        editDisabled: true,
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入是否警告'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    }
    ].concat(vm.commonEndColumns({ update_datetime: { showForm: false, showTable: false }, create_datetime: { showForm: false, showTable: true } }))
  }
}
